//
//  ScreenRulerOverlayScreenWindow.m
//  ScreenRuler
//
//  Created by Tianzhou on 08-12-23.
//  Copyright 2008 Tianzhou Software Inc.. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.

#import "ScreenRulerOverlayScreenWindow.h"


@implementation ScreenRulerOverlayScreenWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag 
{
	//Call NSWindow's version of this function, but pass in the all-important value of NSBorderlessWindowMask
    //for the styleMask so that the window doesn't have a title bar
    NSWindow* result = [super initWithContentRect:contentRect styleMask:NSTexturedBackgroundWindowMask backing:NSBackingStoreBuffered defer:flag];
    //Set the background color to clear so that (along with the setOpaque call below) we can see through the parts
    //of the window that we're not drawing into
	[result setBackgroundColor: [NSColor colorWithCalibratedWhite:1.0 alpha:0.0]];
    //This next line pulls the window up to the front on top of other system windows.  This is how the Clock app behaves;
    //generally you wouldn't do this for windows unless you really wanted them to float above everything.
    //Let's start with no transparency for all drawing into the window
    //but let's turn off opaqueness so that we can see through the parts of the window that we're not drawing into
    [result setOpaque:NO];
    //and while we're at it, make sure the window has a shadow, which will automatically be the shape of our custom content.
    [result setHasShadow: YES];
	
	[result setAcceptsMouseMovedEvents:YES];
	
    return result;
}

- (BOOL)canBecomeKeyWindow
{
    return YES;
}

- (void)mouseDragged:(NSEvent *)theEvent
{
	NSRect theVisibleScreenFrame = [[NSScreen mainScreen] visibleFrame];
	NSPoint theCurrentPoint = [theEvent locationInWindow];
	
	// if the event has no window,it is already in screen coordinate
	if ([theEvent window] != nil)
	{
		theCurrentPoint = [self convertBaseToScreen:theCurrentPoint];
	}
	
	NSPoint newOrigin = NSMakePoint(theCurrentPoint.x - dragStartPoint_.x, theCurrentPoint.y - dragStartPoint_.y);
	
	if ([self frame].size.height + newOrigin.y >= theVisibleScreenFrame.origin.y + theVisibleScreenFrame.size.height)
	{
		newOrigin.y = theVisibleScreenFrame.origin.y + theVisibleScreenFrame.size.height - [self frame].size.height;
	}
	
	[self setFrameOrigin:newOrigin];
}

- (void)mouseDown:(NSEvent *)theEvent
{
	dragStartPoint_ = [self convertBaseToScreen:[theEvent locationInWindow]];
	dragStartPoint_.x -= [self frame].origin.x;
	dragStartPoint_.y -= [self frame].origin.y;
}

- (void)keyDown:(NSEvent *)theEvent
{
    int characterIndex;
    int charactersInEvent;
	
    charactersInEvent = [[theEvent characters] length];
    for (characterIndex = 0; characterIndex < charactersInEvent; characterIndex++)
	{
        if ([[theEvent characters] characterAtIndex:characterIndex] == NSUpArrowFunctionKey)
		{ 
			NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
			id values = [defaults values];
			[values setValue:[NSNumber numberWithInt:kTop]
					  forKey:kScreenRulerHorizontalRulerLocationKey];
			
			// jiggle the window to force synchronize, otherwise the original ruler would still 
			// shown on the screen
			NSRect oldFrame = [self frame];
			[self setFrame:NSMakeRect(oldFrame.origin.x
									  ,oldFrame.origin.y
									  ,oldFrame.size.width + 1
									  ,oldFrame.size.height + 1)
				   display:NO];
			[self setFrame:oldFrame
				   display:YES];
		}
        else if ([[theEvent characters] characterAtIndex:characterIndex] == NSDownArrowFunctionKey)
		{
			NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
			id values = [defaults values];
			[values setValue:[NSNumber numberWithInt:kBottom]
					  forKey:kScreenRulerHorizontalRulerLocationKey];
			
			// jiggle the window to force synchronize, otherwise the original ruler would still 
			// shown on the screen
			NSRect oldFrame = [self frame];
			[self setFrame:oldFrame display:YES];
			[self setFrame:NSMakeRect(oldFrame.origin.x
			                                 ,oldFrame.origin.y
			                                 ,oldFrame.size.width + 1
			                                 ,oldFrame.size.height + 1)
				   display:NO];
			[self setFrame:oldFrame
				   display:YES];

		}
    }
	
}


@end
